<?php

namespace App\Controller;

use Mini\App;
use Mini\AsyncHttpClient;
use Mini\Timer;

class HomeController
{
    public function index()
    {
        $client = new AsyncHttpClient();

        $urls = [
            'http://192.168.3.243:8000/my?id=1',
            'http://192.168.3.243:8000/my?id=2',
        ];

        $start = microtime(true);

        try {
            foreach ($urls as $url) {
                $client->get($url)
                    ->then(function ($response) {
                        echo "成功: \n$response\n";
                    })
                    ->catch(function ($error) {
                        echo "失败: {$error->getMessage()}\n";
                    });
            }
             $client->run();
        } catch (\Throwable $e) {
            var_dump($e->getMessage());
        }

        $end = microtime(true);

        echo "总耗时: " . ($end - $start) . " 秒\n";

    }

    public function my()
    {
        $id = $_GET['id'] ?? 0;
        echo "My! {$id}";
    }
}